<script type="text/javascript">
  function appDiagnosticApply() {
    var appId = $('#appDiagnostic_appId').selectpicker('val');
    if (appId == null || appId == '') {
      alert("请选择应用");
      $('#appDiagnostic_appId').focus();
      return false;
    }

    var type = document.getElementById("diagnostic_type");
    if (type.value == "") {
      alert("请选择诊断类型!");
      type.focus();
      return false;
    }

    var nodeInfos = $('#instance-select').selectpicker('val');

    var reason = document.getElementById("appDiagnosticReason");
    if (reason.value == "") {
      alert("请填写原因!");
      reason.focus();
      return false;
    }
    if(!confirm("确认提交应用诊断申请？")){
      return ;
    }

    var btn = document.getElementById("appDiagnosticBtn");
    btn.disabled = true;

    $.post(
            '${request.contextPath}/admin/app/job/submit',
            {
              jobType: 8,
              appId: appId,
              param: type.value,
              nodeInfos: nodeInfos == null ? "" : nodeInfos.toString(),
              reason: reason.value
            },
            function (data) {
              if (data == 1) {
                $("#appDiagnosticInfo").html("<div class='alert alert-error' ><font color='green'><strong>Success!</strong>应用诊断申请提交成功，即将跳转工单列表！</font><button class='btn-close' data-bs-dismiss='alert'></button></div>");
                setTimeout("location.href = '${request.contextPath}/admin/app/jobs'", 1000);
              } else {
                btn.disabled = false;
                $("#appDiagnosticInfo").html("<div class='alert alert-error' ><font color='red'><strong>Error!</strong>更新失败！<font><button class='btn-close' data-bs-dismiss='alert'></button></div>");
              }
            }
    );
  }
</script>

<div class="card-header">
  <h4 class="card-title">
    诊断应用
  </h4>
</div>

<div class="card-body">
    <div class="row">
      <div class="form-group row">
        <label class="col-form-label col-md-2 offset-md-1">应用<font color='red'>(*)</font></label>
        <div class="col-md-5">
          <select id="appDiagnostic_appId" name="appId" class="selectpicker show-tick col-md-12 border rounded"
                  data-live-search="true" title="选择应用"
                  onchange="changeAppIdSelect(this.value,'instance-select', '${request.contextPath}')">
            <option value="">选择应用</option>
            <#list appDescMap?keys as key>
              <#assign appDesc = appDescMap?api.get(key)>
              <#if (appDesc.status==2)>
                <option value="${appDesc.appId}" title="${appDesc.appId} ${appDesc.name}">
                  【${appDesc.appId}】&nbsp;名称：${appDesc.name}&nbsp;类型：${appDesc.typeDesc}&nbsp;版本：${appDesc.versionName}
                </option>
              </#if>
            </#list>
          </select>
        </div>
      </div>

      <div class="form-group row">
        <label class="col-form-label col-md-2 offset-md-1">实例</label>
        <div class="col-md-5">
          <select id="instance-select" name="nodes"
                  class="selectpicker col-md-12 border rounded" multiple
                  data-live-search="true" title="选择实例 (默认全部)" data-size="8">
          </select>
        </div>
      </div>

      <div class="form-group row">
        <label class="col-form-label col-md-2 offset-md-1">诊断类型</label>
        <div class="col-md-5">
          <select id="diagnostic_type" name="type" class="form-select">
            <#list diagnosticTypeMap?keys as key>
              <option value="${diagnosticTypeMap?api.get(key)}">
                ${diagnosticTypeMap?api.get(key)}
              </option>
            </#list>
          </select>
        </div>
      </div>

      <div class="form-group row">
        <label class="col-form-label col-md-2 offset-md-1">申请原因/描述<font color='red'>(*)</font></label>
        <div class="col-md-5">
              <textarea rows="6" name="appDiagnosticReason" id="appDiagnosticReason"
                        placeholder="scan：扫描匹配的键，如abc*&#13;&#10;memoryUsed：扫描大内存键，如内存大于1MB&#13;&#10;idlekey：扫描空闲时间长的键，如7天&#13;&#10;hotkey：扫描热点键&#13;&#10;deletekey：删除匹配的键，如abc*&#13;&#10;slotAnalysis：分析集群槽的键分布&#13;&#10;sampleCompare：数据抽样比对&#13;&#10;realTimeMonitor：实时监控"
                        class="form-control"></textarea>
        </div>
      </div>

      <div class="form-group row">
        <div class="text-center">
          <button id="appDiagnosticBtn" class="btn btn-info" onclick="appDiagnosticApply()">
            <i class="bi bi-check"></i>
            提交申请
          </button>
        </div>
      </div>

      <div class="form-group row">
        <div id="appDiagnosticInfo" class="offset-md-3 col-md-9"></div>
      </div>

    </div>

</div>
